package com.huiyujiaoyu.powertime.ui.my;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.huiyujiaoyu.base.BaseActivity;
import com.huiyujiaoyu.http.HttpApi;
import com.huiyujiaoyu.http.HttpMethods;
import com.huiyujiaoyu.powertime.R;
import com.huiyujiaoyu.powertime.model.UserInfoBean;
import com.huiyujiaoyu.util.ActivityUtil;
import com.huiyujiaoyu.widget.LlkjToolBar;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by win7 on 2017/6/18.
 * 修改个性签名
 */

public class UpdataDescActivity extends BaseActivity {

    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.toolbar)
    LlkjToolBar mToolbar;
    private Map<String, Object> params;
    private String sign;
    public static void launch(Context context) {
        ActivityUtil.startActivity(context, UpdataDescActivity.class);
    }

    @BindView(R.id.et_desc)
    EditText etDesc;
    @BindView(R.id.btn_save)
    Button btnSave;

    @Override
    protected void initView() {
        super.initView();
        setToolbar(mToolbar);
        mToolbar.setToolBar("个性签名", true, "", R.mipmap.icon_titlebar_back, false, "", LlkjToolBar.isNull);
        etDesc.setText(UserInfoBean.getInstance().getSignature());
        tvNum.setText(etDesc.getText().length()+"/30");
    }

    @Override
    protected void initListener() {
        super.initListener();
        etDesc.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String content = etDesc.getText().toString().trim();
                int le = content.length();
                tvNum.setText(le + "/30");
            }
        });
    }

    @OnClick({R.id.btn_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_save:
                sign = etDesc.getText() + "";
                if (params == null) {
                    params = new HashMap<>();
                }
                params.put("intro", sign);
                HttpMethods.getInstance().update(mContext, getComp(), this, params);
                break;
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_updata_desc;
    }
    @Override
    public void onNext(Object o, int httpcode) {
        super.onNext(o, httpcode);
        switch (httpcode) {
            case HttpApi.HTTP_UPDATE:
                UserInfoBean.getInstance().setSignature(sign);
                finish();
                break;
        }
    }
}
